package def
{	
	import com.friendsofed.isometric.IsoObject;
	import com.greensock.TweenMax;
	
	import core.Menu;
	import core.XUIGroup;
	import core.layer.XStageLayer;
	
	import flash.display.DisplayObject;
	import flash.display.Sprite;
	import flash.display.Stage;
	
	import org.puremvc.as3.interfaces.IFacade;
	import org.puremvc.as3.interfaces.IMediator;
	import org.puremvc.as3.patterns.facade.Facade;
	import org.puremvc.as3.patterns.mediator.Mediator;

	public class XG
	{
		/***游戏舞台****************/
		static public var stage :  Stage;
		
		static public var mainThis : Sprite;
		
		/**游戏Facade**/
		static public var facade : IFacade;
		
		static public const stageLayer : XStageLayer = XStageLayer.Instance;
		
		public static var menu:Menu = Menu.Instance;
//==============================================================================================================================		
		public function XG(facade:IFacade,mainThis:Sprite)
		{
			init(facade,mainThis);
		}
		
		public function init(facade:IFacade,mainThis:Sprite) : void
		{
			XG.facade = facade;
			XG.stage = mainThis.stage; 
			XG.mainThis = mainThis;
			
			stageLayer.init(stage);
		}
		
		/**
		 * 
		 * @param entity    
		 * @param targetEntity
		 * 比如  场景场景跟这人移动  人为entity 场景为targetEntity
		 */		
		static public function follow(entity:DisplayObject,targetEntity:DisplayObject,speed:Number):void
		{
			
		}
		
		static public function addToWorld(entity : IsoObject) : void
		{
			stageLayer.world.addChildToWorld(entity);
		}
		
		static public function showMouseHand(hand : DisplayObject) : void
		{
			stageLayer.mouse.show(hand);
		}
		
		static public function hideMouseHand() : void
		{
			stageLayer.mouse.clear();
		}
		
		static public function addToPopup() : void
		{
			
		}
//=============================================================================================================================
		/**
		 * 舞台鼠标x位置
		 * @return  
		 */		
		static public function get mouseX():Number
		{
			return stage.mouseX;
		}
		
		/**
		 * 舞台鼠标y位置
		 * @return  
		 */	
		static public function get mouseY():Number
		{
			return stage.mouseY;
		}
//=============================================================================================================================
		/**
		 * 判断是否有该UI
		 * **/
		static public function hasUI(mediatorName : String) : Boolean
		{
			return stageLayer.ui.uiGroup.hasMeditor(mediatorName);
		}
		
		/**
		 * 销毁所有UI
		 * **/
		static public function destoryAllUI() : void
		{
			for each(var ui : Sprite in stageLayer.ui.uiGroup.group)
			{
				stageLayer.ui.removeChild(ui);
			}
			stageLayer.ui.uiGroup.destoryAll();
		}
		
		/**
		 * 使目标UI到最成为最上层UI
		 * **/
		static public function swapUI(mediatorName : String) : void
		{
			var targetUI:Sprite = stageLayer.ui.uiGroup.group[mediatorName];
			var targetIndex : int = stageLayer.ui.getChildIndex(targetUI);
			
			if(targetIndex == stageLayer.ui.numChildren-1 ) return;
			
			var uiGroupA : Array = []; 
			for each(var ui:Sprite in stageLayer.ui.uiGroup.group)
			{
				var uiIndex : int = stageLayer.ui.getChildIndex(ui);
				
				uiGroupA.push({ui:ui,depth:uiIndex});
			} 
			
			uiGroupA.sortOn("depth",Array.NUMERIC);
			
			for(var i:int =0;i<uiGroupA.length;i++)
			{
				uiIndex = uiGroupA[i]["depth"];
				
				if(uiIndex > targetIndex)
				{
					stageLayer.ui.swapChildrenAt(uiIndex,targetIndex);
				}
			}
		}
		
		/**
		 * 显示UI
		 * **/
		static public function openUI(mediatorName : String,mediatorClassRef:*=null) : void
		{
			if(!stageLayer.ui.uiGroup.group[mediatorName])
			{	
				if(mediatorClassRef)
				{
					var mediator : Mediator = new mediatorClassRef; 
					facade.registerMediator(mediator);
				}
				
				if(facade.hasMediator(mediatorName))
				{
					stageLayer.ui.uiGroup.add(mediatorName,facade.retrieveMediator(mediatorName).getViewComponent() as Sprite);
					stageLayer.ui.addChild(stageLayer.ui.uiGroup.group[mediatorName]);
				}
			}
			else
			{
				trace("already open this facade");
			}
		}
		
		/**
		 * 隐藏UI
		 * **/
		static public function closeUI(mediatorName : String) : void
		{
			if(!stageLayer.ui.uiGroup.group[mediatorName])
			{
				trace("already close this facade");
			}
			else
			{
				stageLayer.ui.removeChild(stageLayer.ui.uiGroup.group[mediatorName]);
				stageLayer.ui.uiGroup.remove(mediatorName);
				
				facade.removeMediator(mediatorName);
			}
		}
		
		/**
		 * 循环显示隐藏UI
		 * **/
		static public function turnUI(mediatorName : String) : void
		{
			if(!stageLayer.ui.uiGroup.group[mediatorName])
			{
				stageLayer.ui.uiGroup.add(mediatorName,facade.retrieveMediator(mediatorName).getViewComponent() as Sprite);
				stageLayer.ui.addChild(stageLayer.ui.uiGroup.group[mediatorName]);
			}
			else
			{
				stageLayer.ui.removeChild(stageLayer.ui.uiGroup.group[mediatorName]);
				stageLayer.ui.uiGroup.remove(mediatorName);
			}
		}
		
		/**
		 * 获取UI 
		 * @param mediatorName
		 * @return Mediator
		 */		
		static public function getUI(mediatorName : String) : *
		{
			return facade.retrieveMediator(mediatorName);
		}
	}
}